package com.util;

public class ConverterUnit {
    // Convert char array to String
    public static String char2str(byte[] source, int clen) {
        System.out.println("source的长度是"+source.length);
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < clen; i++) {
            str.append((char) source[i]);
        }
        System.out.println("字节转字符串时影响它的大小："+str.toString().getBytes().length);
        return str.toString();
    }

    // Convert String to char array
    public static int str2char(byte[] output, String str) {
        int len = str.length();
        for (int i = 0; i < len; i++) {
            output[i] = (byte) str.charAt(i);
        }
        return len;
    }
    public static void main(String[] args) {
        byte[] source = {'H', 'e', 'l', 'l', 'o'};
        String resultStr = char2str(source, source.length);
        System.out.println("String: " + resultStr);

        byte[] output = new byte[resultStr.length()];
        int length = str2char(output, resultStr);
        System.out.println("Byte array: " + new String(output) + ", Length: " + length);
    }
}
